<?php

class Question{
	
	
	function _construct($id){
		$this->id = $id;
		
	}
	
	public function getQuestion($id){
		 $sql = "SELECT * FROM `question` WHERE  `id` = $id";
         $result = mysql_query($sql) or die (mysql_error());
         $question = mysql_fetch_object($result)or die("Get Question Details".mysql_error);
         
         if($question){
         return $question;
         }else{
         	return false;
         }
	}
	
	public function deleteQuestion($id){
		$sql = "DELETE FROM `question` WHERE  `id` = '$id'";
	    $result = mysql_query($sql) or die (mysql_error());
		
	    if($result){
	    	return true;
	    }else{
	    	return false;
	    }
	    
	}
	
	public function addQuestion($data){
	  $sql = "INSERT INTO `question` (`survey_id`,`type`,`required`,`question`,`create_section`,`order`,`comment_box`,`response1`,`response2`,`response3`,`response4`,`response5`,`response6`)
			VALUES (
            '{$data['survey_id']}',
            '{$data['type']}',
			'{$data['required']}',
			'{$data['question']}',
			'{$data['create_section']}',
			'{$data['order']}',
			'{$data['comment_box']}',
			'{$data['response1']}',
            '{$data['response2']}',
            '{$data['response3']}',
            '{$data['response4']}',
            '{$data['response5']}',
            '{$data['response6']}'
			);";

	    $result = mysql_query($sql) or die('Add Question: '.mysql_error());
	    
	    if($result){
	    	return true;
	    }else{
	    	return false;
	    }

        }
	
	public function updateQuestion($qid,$data){
	      $sql = "UPDATE `question` SET `type` ='{$data['type']}' ,`required`='{$data['required']}' ,`question` = '{$data['question']}',
	             `create_section`='{$data['create_section']}',`order`='{$data['order']}',`comment_box`='{$data['comment_box']}',`response1`='{$data['response1']}',`response2`='{$data['response2']}',
	             `response3`='{$data['response3']}',`response4`='{$data['response4']}',`response5`='{$data['response5']}',`response6`='{$data['response6']}'
			      WHERE `id`='$qid';";

	      $result = mysql_query($sql) or die('Update Question: '.mysql_error());

          if($result){
      	   return true;
          }else{
      	   return false;
          }  

      }
      
   public function checkCondition($id){
   	$sql = "SELECT * FROM `branching` WHERE `question_1_id` = '$id'";
   	$result = mysql_query($sql)or die("Check Condition:".mysql_error());
    $i=0;
   	while($c = mysql_fetch_object($result)){
   		$condition[$i]['question_2_id'] = $c->question_2_id;
   		$condition[$i]['condition']=$c->condition;
   		$condition[$i]['condition_value']= $c->value;
   		$i++;
   	}
   	if($condition){
   	return $condition;
   	}else{
   		return false;
   	}
   	
   }
   
   public function deleteCondition($id){
   	$sql = "DELETE FROM `branching` WHERE `id`='$id'";
   	$result = mysql_query($sql) or die("Delete Condition:".mysql_error());
   }

   public function getOrder($id){
   	$sql = "SELECT `order` FROM `question` WHERE `id`='$id'";
   	$result = mysql_query($sql);
   	$order = mysql_fetch_object($result);
   	if($order){
   		return $order;
   	}else{
   		return false;
   	}
   }
   
   public function updateOrder($id,$order){
   	$sql = "UPDATE `question` SET `order`='$order' WHERE `id`='$id'";
   	$result = mysql_query($sql) or die("Update Question Order:".mysql_error());
   	if($result){
   		return true;
   	}else{
   		return false;
   	}
   }
   
   public function getQuestionByOrder($survey_id,$order){
   	
    $sql = "SELECT `id` FROM `question` WHERE `survey_id`='$survey_id' AND `order`='$order'";
   	$result = mysql_query($sql);
   	$question = mysql_fetch_object($result);
   	if($question){
   		return $question;
   	}else{
   		return false;
   	}
   }
   
   public function getLargestOrder($survey_id){
   	
   	$sql = "SELECT MAX(`order`) FROM `question` WHERE `survey_id`='$survey_id'";
   	$result = mysql_query($sql) or die("cannot fetch maximum order".mysql_error());
   	$l = mysql_fetch_row($result);
   	foreach($l as $key=>$value){
   		$largest_order = $value;
   	}
   	if($largest_order){
   		return $largest_order;
   	}else{
   		return false;
   	}
   }
	
}

?>